#define _CRT_SECURE_NO_WARNINGS 1
#include<vector>
#include<vector>
using namespace std;
class Solution {
    vector<vector<bool>>vis;
    int m, n, count = 1;
    int dx[4] = { 0,0,-1,1 };
    int dy[4] = { -1,1,0,0 };
public:
    int maxAreaOfIsland(vector<vector<int>>& grid)
    {
        int ret = 0;
        m = grid.size();
        n = grid[0].size();
        int tmp = 0;
        vis = vector<vector<bool>>(m, vector<bool>(n));
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
            {
                if (!vis[i][j] && grid[i][j] == 1)
                {
                    vis[i][j] = true;
                    count = 1;
                    dfs(grid, i, j);
                    ret = max(count, ret);
                }
            }
        }
        return ret;
    }
    int dfs(vector<vector<int>> grid, int i, int j)
    {
        for (int k = 0; k < 4; k++)
        {
            int x = i + dx[k], y = j + dy[k];
            if (x >= 0 && x < m && y >= 0 && y < n && !vis[x][y] && grid[x][y] == 1)
            {
                ++count;
                vis[x][y] = true;
                dfs(grid, x, y);
            }
        }
        return count;
    }
};